package com.jiantong.web.open.app.news;

import com.jiantong.news.model.Quotes;
import com.jiantong.news.service.QuotesReadService;
import com.jiantong.news.service.QuotesWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;

/**行情报价
 * Created by mz03 on 2017/3/16.
 *@author :zyp
 */

@Slf4j
@OpenBean
public class QuotesApis {
    @RpcConsumer
    private QuotesWriteService quotesWriteService;

    @RpcConsumer
    private QuotesReadService quotesReadService;

    /**
     * 根据ID 查询一条行情报价
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.quotes.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public Quotes findById(Long id){

        Response<Quotes> response = quotesReadService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find quotes read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 根据type拿到所有省份的最新报价
     * @param type
     * @return
     */
    @OpenMethod(
            key = "fin.quotesList.by.type",
            paramNames = {"type"},
            httpMethods = RequestMethod.GET
    )
    public List<Quotes> findByType(Integer type){
       Response<List<Quotes>> response = quotesReadService.findByType(type);
        if(!response.isSuccess()){
            log.error("failed to find List<Quotes> read by type = {}, cause : {}",type,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();

    }

    /**
     * 根据state查询(查询全部)
     * @param state
     * @return
     */
    @OpenMethod(
            key = "fin.quotesAll.by.state",
            paramNames = {"state"},
            httpMethods = RequestMethod.GET
    )
    public List<Quotes> findByState(Integer state){
        Response<List<Quotes>> response = quotesReadService.findByState(state);
        if (!response.isSuccess()){
            log.error("failed to find allQutes read by state = {}, cause : {}",state,response.getError());
            throw new OPServerException(response.getError());
        }
        return  response.getResult();
    }

    /**
     * 根据省查找最新的一组报价
     * @param province
     * @return
     */
    @OpenMethod(
            key = "fin.quotesNewProvince.by.province",
            paramNames = {"province"},
            httpMethods = RequestMethod.GET
    )
    public List<Quotes> findByNewProvince(String province){
        Response<List<Quotes>> response = quotesReadService.findByNewProvince(province);
        if (!response.isSuccess()){
            log.error("failed to find quotesNewProvince read by province = {}, cause : {}",province,response.getError());
            throw new OPServerException(response.getError());
        }
        return  response.getResult();
    }

    /**
     * 根据类型type和时间查询当前时间内的跟新
     * @param type
     * @param date
     * @return
     */
    @OpenMethod(
            key = "fin.quotesNewType.by.type",
            paramNames = {"type","date"},
            httpMethods = RequestMethod.GET
    )
    public List<Quotes> findByNewType(Integer type, String date){
        Date date1 = null;
        if(!Strings.isNullOrEmpty(date)){
            date1 = DateTime.parse(date).toDate();
        }
        Response<List<Quotes>> response = quotesReadService.findByNewType(type,date1);
        if (!response.isSuccess()){
            log.error("failed to find QuotesNewType read by type = {}, date = {}, cause : {}",type,date,response.getError());
            throw new OPServerException(response.getError());
        }
        return  response.getResult();
    }

    /**
     * 根据type和省份查询15天内的价格
     * @param type
     * @param province
     * @return
     */
    @OpenMethod(
            key = "fin.findBy15Day.by.province type",
            paramNames = {"type","province"},
            httpMethods = RequestMethod.GET
    )
    public List<Quotes> findBy15Day(Integer type,String province){
        Response<List<Quotes>> response = quotesReadService.findBy15Day(type,province);
        if(!response.isSuccess()){
            log.error("failed to find findBy15Day read by type = {},province = {}, cause:{}",type,province,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /*
   创建一条信息
    */
    @OpenMethod(
            key = "create.quotes",
            paramNames = {"quotes"},
            httpMethods = RequestMethod.POST
    )
    public Long create(Quotes quotes){
        Response<Long> response = quotesWriteService.create(quotes);
        if(!response.isSuccess()){
            log.error("failed to create quotes read by quotes = {}, cause : {}", quotes, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


}
